//
//  NSMutableArray+AFNetGet.m
//  FootBallTest
//
//  Created by lanou3g on 15/5/29.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "NSMutableArray+AFNetGet.h"
#import "AFHTTPRequestOperationManager.h"
#import "NewsSectionModel.h"
#import "NewsModel.h"
#import "MJRefresh.h"
@implementation NSMutableArray (AFNetGet)

+ (void)getMutableArrayWithUrl:(NSString *)url invokeDay:(NSString*)invokeDay tableView:(UITableView *)tableView success:(void (^)(NSMutableArray * returnArray,NSString *data_block ,NSString *lastDay))success{
    NSMutableArray *array = [NSMutableArray array];
    
    
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *dict = @{@"format":@"json"};
     typeof(array) __block weakArray = array;
    __weak typeof(tableView) weakTableView = tableView;
        [manager GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            if (responseObject) {
                
          
            
            
            NSDictionary * dayLimit = [[responseObject objectForKey:@"result"] objectForKey:@"days"];
            NSString * minDay = [dayLimit objectForKey:@"min"];
            NSString * maxDay = [dayLimit objectForKey:@"max"];
            
            NSInteger minDayInteger = [minDay integerValue];
            NSInteger maxDayInteger = [maxDay integerValue];
            
         
            
                
                
            if ((([invokeDay integerValue]>minDayInteger &&[invokeDay integerValue]<maxDayInteger))||[invokeDay isEqualToString:@"1"]) {
           
                
     
        
        NSArray *dataDictArray= [[responseObject objectForKey:@"result"] objectForKey:@"games"];
        
            
            
        for (NSDictionary *littleDict in dataDictArray) {
            
            
            NewsSectionModel *weakModel = [[NewsSectionModel alloc]init];
            weakModel.array = [NSMutableArray array];
            [weakModel setValuesForKeysWithDictionary:littleDict];
        
            NSMutableArray *newsModelArray = [littleDict objectForKey:@"data"];
            
            
            for (NSDictionary *newsModelDic in newsModelArray) {
                NewsModel *model = [[NewsModel alloc]init];
                [model setValuesForKeysWithDictionary:newsModelDic];
                [weakModel.array addObject:model];
            }
            
            
            [weakArray addObject:weakModel];
        }
            
            NewsSectionModel *sectionModel = weakArray.firstObject;
            NewsSectionModel *lastModel = weakArray.lastObject;
                
            
            
            NSString *data_block = sectionModel.firstDay;
            NSString *lastDay = [NSString stringWithFormat:@"%ld",[lastModel.firstDay integerValue] +1];
            
                if ( weakArray.count ) {
                     success(weakArray,data_block,lastDay);
                }
                
           
                
            }else{
                [weakTableView headerEndRefreshing];
                [weakTableView footerEndRefreshing];
            }
                
            }else{
                [weakTableView headerEndRefreshing];
                [weakTableView footerEndRefreshing];
            }
            
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
       
        
    }];
        
    
  
}



@end
